package tech.aistar.service.noteshow.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.Notes;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.mapper.NotesMapper;
import tech.aistar.service.noteshow.INoteShowService;
import tech.aistar.vo.noteshow.NotesShowVo;

import java.util.*;

/**
 * Created by Dell on 2019/9/28.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class NoteShowServiceImpl implements INoteShowService {
    @Autowired
    private NotesMapper notesMapper;
    @Override
    public List<NotesShowVo> getNotesAll() {
        List<NotesShowVo> notesShowVos = new ArrayList<>();
        //获取所有的文章
        List<Notes> notesList = notesMapper.getAllNotes();

        //遍历所有文章
        Iterator<Notes> iter = notesList.iterator();
        while (iter.hasNext()){
            Notes note = iter.next();
            NotesShowVo notesShowVo = new NotesShowVo();
            //保存文章信息
            notesShowVo.setNotes(note);

            NoteAuthor noteAuthor = notesMapper.findAll(note);
            //取出用户名
            UserTable userTable = notesMapper.getId(noteAuthor);

            notesShowVo.setUserTable(userTable);

            notesShowVo.setNoteAuthor(noteAuthor);

            notesShowVos.add(notesShowVo);
        }
        return notesShowVos;
    }

    /**
     * @Description:查询游记内容
     * @param:
     * @author XiaoDi
     * @date 2019/10/2 15:49
     */
    @Override
    public Map<String,Object> getNotesAll(String title,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //获取所有文章进行分页
        List<Notes> notes = notesMapper.getfindNotePlace( title);

        PageInfo<Notes> notesPageInfo = new PageInfo<>(notes);

        //定义一个集合
        List<NotesShowVo> notesShowVos = new ArrayList<>();

        //获取分页之后的集合
        List<Notes> notesList = notesPageInfo.getList();

        //遍历所有文章
        Iterator<Notes> iter = notesList.iterator();
        while (iter.hasNext()){
            Notes note = iter.next();
            NotesShowVo notesShowVo = new NotesShowVo();
            //保存文章信息
            notesShowVo.setNotes(note);

            NoteAuthor noteAuthor = notesMapper.findAll(note);
            //取出用户名
            UserTable userTable = notesMapper.getId(noteAuthor);

            notesShowVo.setUserTable(userTable);


            notesShowVo.setNoteAuthor(noteAuthor);

            notesShowVos.add(notesShowVo);
        }

        //使用map集合来封装第三方实体类的数据集合,和分页集合

        Map<String,Object> map = new HashMap<>();
        map.put("notesPageInfo",notesPageInfo);
        map.put("notesShowVos",notesShowVos);


        return map;

    }



    /**
     * @Description:游记内容分页,由于PageHelper是对第一个查询条件分页,
     * 1- -先对主要显示的内容进行分页
     * 2- - 在对查询第三方实体类的数据进行处理
     * 3- -把两个对象封装到map集合里面
     * @param:
     * @author XiaoDi
     * @date 2019/10/5 18:03
     */
    @Override
    public Map<String, Object> getNoteAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //获取所有文章进行分页
        List<Notes> notes = notesMapper.getAllNotes();
        PageInfo<Notes> notesPageInfo = new PageInfo<>(notes);

        //定义一个集合
        List<NotesShowVo> notesShowVos = new ArrayList<>();

        //获取分页之后的集合
        List<Notes> notesList = notesPageInfo.getList();

        //遍历所有文章
        Iterator<Notes> iter = notesList.iterator();
        while (iter.hasNext()){
            Notes note = iter.next();
            NotesShowVo notesShowVo = new NotesShowVo();
            //保存文章信息
            notesShowVo.setNotes(note);

            NoteAuthor noteAuthor = notesMapper.findAll(note);
            //取出用户名
            UserTable userTable = notesMapper.getId(noteAuthor);

            notesShowVo.setUserTable(userTable);


            notesShowVo.setNoteAuthor(noteAuthor);

            notesShowVos.add(notesShowVo);
        }

        //使用map集合来封装第三方实体类的数据集合,和分页集合

        Map<String,Object> map = new HashMap<>();
        map.put("notesPageInfo",notesPageInfo);
        map.put("notesShowVos",notesShowVos);


        return map;
    }
}
